package com.renwei.yunlong.fragment;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.fingdo.statelayout.StateLayout;
import com.google.gson.Gson;
import com.renwei.yunlong.R;
import com.renwei.yunlong.adapter.KnowCommentAdapter;
import com.renwei.yunlong.base.BaseRefreshFragment;
import com.renwei.yunlong.bean.KnowCommentBean;
import com.renwei.yunlong.event.KnowCommentRefreshEvent;
import com.renwei.yunlong.event.KnowCommentReplyEvent;
import com.renwei.yunlong.http.HttpTaskListener;
import com.renwei.yunlong.utils.JsonMapUtil;
import com.renwei.yunlong.utils.ServiceRequestManager;
import com.renwei.yunlong.view.SimpleOptionView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;
import com.zyf.fwms.commonlibrary.base.baseadapter.BaseRecyclerViewAdapter;

import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;
import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;
import de.greenrobot.event.ThreadMode;

public class KnowCommentFrament extends BaseRefreshFragment implements HttpTaskListener, BaseRecyclerViewAdapter.OnItemClickLitener, OnRefreshLoadmoreListener {

    @Bind(R.id.simpleTileView)
    SimpleOptionView simpleTileView;
    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;
    @Bind(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @Bind(R.id.state_layout)
    StateLayout stateLayout;

    private String knowledgeId;
    private int page;
    private KnowCommentAdapter adapter;
    private String flag = "1";

    public KnowCommentFrament(String knowledgeId) {
        this.knowledgeId = knowledgeId;
    }

    @Override
    protected int getLayoutId() {
        EventBus.getDefault().register(this);
        return R.layout.fragment_know_comment;

    }

    @Override
    public void onPageCheck() {
        refreshLayout.autoRefresh();
    }

    protected void initView() {
        page = 1;
        simpleTileView.setTitle("评论");

        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        adapter = new KnowCommentAdapter(getContext(), companyType);
        recyclerView.setAdapter(adapter);
        refreshLayout.setOnRefreshLoadmoreListener(this);


        adapter.setOnItemClickLitener(this);
    }

    private void initData() {
        HashMap<String, String> map = new HashMap<>();
        if ("1".equals(companyType)) {
            map.put("recordUserId", ownerBean.getRows().getEmployeeId());
        } else if ("2".equals(companyType)) {
            map.put("recordUserId", serviceLoginBean.getRows().getEmployeeId());
        }
        map.put("knowledgeId", knowledgeId);
        map.put("flag", flag);//2按时间查询  1按热度查询
        map.put("page", page + "");
        map.put("rows", "20");
        ServiceRequestManager.getManager().getKnowCommentList(getContext(), JsonMapUtil.mapToJson(map), this);
    }

    @Subscribe(threadMode = ThreadMode.MainThread)
    public void onCommentRefreshEvent(KnowCommentRefreshEvent event) {
        page = 1;
        adapter.clean();
        initData();
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        initView();
        initData();
    }

    @Override
    public void onLoadmore(RefreshLayout refreshlayout) {
        page++;
        initData();
    }

    @Override
    public void onItemClick(View view, int position) {
        KnowCommentBean.RowsBean bean = adapter.getItem(position);
        switch (view.getId()) {
            case R.id.tv_reply: // 回复
                if (bean != null) {
                    EventBus.getDefault().post(new KnowCommentReplyEvent(bean.getId() + "",bean.getRecordUserName()));
                }
                break;
            case R.id.ll_up:// 点赞
                if (bean != null) {
                    setUp(bean);
                }
                break;
            case R.id.tv_order:
                if ("1".equals(flag)) {
                    flag = "2";
                } else if ("2".equals(flag)) {
                    flag = "1";
                }
                page = 1;
                adapter.clean();
                initData();
                break;
        }
    }

    private void setUp(KnowCommentBean.RowsBean bean) {
        HashMap<String, String> map = new HashMap<>();
        if ("1".equals(companyType)) {
            map.put("recordUserId", ownerBean.getRows().getEmployeeId());
        } else if ("2".equals(companyType)) {
            map.put("recordUserId", serviceLoginBean.getRows().getEmployeeId());
        }
        map.put("id", bean.getId() + "");
        map.put("knowledgeId", knowledgeId);
        ServiceRequestManager.getManager().setKnowCommentUp(getContext(), JsonMapUtil.mapToJson(map), this);
    }

    @Override
    public void onSuccess(int requestId, String result) {
        switch (requestId) {
            case 1:
                refreshLayout.finishRefresh();
                refreshLayout.finishLoadmore();
                KnowCommentBean knowCommentBean = new Gson().fromJson(result, KnowCommentBean.class);
                if (knowCommentBean.getMessage().getCode() == 200) {
                    adapter.addAll(knowCommentBean.getRows());
                    if (knowCommentBean.getRows().size() < 20) {
                        refreshLayout.finishLoadmoreWithNoMoreData();
                    }
                    if (adapter.getData().size() == 0) {
                        stateLayout.showEmptyView();
                    } else {
                        refreshLayout.resetNoMoreData();
                        stateLayout.showContentView();
                    }
                }
                break;
            case 2:
                break;
        }
    }

    @Override
    public void onException(int requestId, String code) {
        switch (requestId) {
            case 1:
                refreshLayout.finishRefresh();
                refreshLayout.finishLoadmore();
                page--;
                break;
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
        ButterKnife.unbind(this);
    }

}
